package com.udaos.modules.card.services;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.udaos.common.base.Service;
import com.udaos.models.card.Ud_card;
import com.udaos.models.card.Ud_card_template;
import com.udaos.models.card.Ud_wx_card;
import com.udaos.models.sys.Sys_menu;
import com.udaos.modules.card.vo.WxCardVo;

/**
 * 卡券service
 * @author Administrator
 *
 */
@IocBean(args = { "refer:dao" })
public class UdCardService extends Service<Ud_card>{
	@Inject
	private UdWxCardService cardInfoService;
	@Inject
	private UdCardTemplateService couponModelService;
	public UdCardService(Dao dao) {
		super(dao);
	}

	/**
	 * 获取卡券名称
	 * @param rawid
	 * @param source
	 * @return
	 */
	public String getCardName(String rawid, String source) {
		if(source.equals("微信")){
			Ud_wx_card card_info=cardInfoService.fetch(Cnd.where("card_id", "=", rawid));
			if(null !=card_info){
				return card_info.getTitle();
			}
		}else if(source.equals("平台")){
			Ud_card_template coupon_model=couponModelService.fetch(rawid);
			if(null !=coupon_model){
				return coupon_model.getCoupon_name();
			}
		}
		return null;
	}

	public List<WxCardVo> queryOn() {
		String sqls="SELECT c.rawid AS rawid,i.title AS title  "+
					" FROM ud_card c inner JOIN ud_wx_card i on c.rawid = i.card_id  "+
					" WHERE c.state=1 AND c.source='微信' AND c.delFlag=0 ORDER BY c.opAt";
		Sql sql =Sqls.create(sqls);
		Entity<WxCardVo> entity = dao().getEntity(WxCardVo.class);
		sql.setEntity(entity);
        sql.setCallback(Sqls.callback.entities());
        dao().execute(sql);
        return sql.getList(WxCardVo.class);
	}
}
